﻿/********************************************************
 * 类名： InitPurBLL.cs
 * 功能： 购货单管理业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-12-20	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using PSS.DAL.CommonManage;
using PSS.DAL.InitPurManage;
using PSS.Entity.InitPurManage;
using PSS.Utility;

namespace PSS.BLL.InitPurManage
{
    public class InitPurBLL
    {
        private readonly InitPurDAL _dal = new InitPurDAL();

        private readonly InitPurDetailDAL _idal = new InitPurDetailDAL();

        private readonly InventoryDAL _indal = new InventoryDAL();

        public string GetInitPurList(InitPurQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetInitPurList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public void AddInitPur(string data)
        {
            var model = new InitPurEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<InitPurDetailEntity>();
            var ilist = new List<InventoryEntity>();
            foreach (Hashtable row in rows)
            {
                model.InitPurId = Guid.NewGuid();
                model.VendorId = new Guid(row["VendorId"].ToString());
                model.DocDate = Convert.ToDateTime(row["DocDate"].ToString());
                model.DocNumber = row["DocNumber"].ToString();
                model.BusinessCategory = Convert.ToInt32(row["BusinessCategory"]);
                model.Remark = row["Remark"] != null ? row["Remark"].ToString() : string.Empty;
                model.DiscountRate = row["DiscountRate"] != null ? Convert.ToDecimal(row["DiscountRate"].ToString()) : 0;
                model.DeductionAmount = row["DeductionAmount"] != null ? Convert.ToDecimal(row["DeductionAmount"].ToString()) : 0;
                model.FoldAmount = row["FoldAmount"] != null ? Convert.ToDecimal(row["FoldAmount"].ToString()) : 0;
                model.Payment = row["Payment"] != null ? Convert.ToDecimal(row["Payment"].ToString()) : 0;
                model.AccountId = row["AccountId"] != null ? new Guid(row["AccountId"].ToString()) : Guid.Empty;
                model.Arrears = row["Arrears"] != null ? Convert.ToDecimal(row["Arrears"].ToString()) : 0;
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {

                    var cmodel = new InitPurDetailEntity();
                    cmodel.InitPurDetailId = Guid.NewGuid();
                    cmodel.InitPurId = model.InitPurId;
                    cmodel.GoodsId = new Guid(item["GoodsId"].ToString());
                    cmodel.Qty = item["Qty"] != null ? Convert.ToDecimal(item["Qty"]) : 0;
                    cmodel.PurchasePrice = item["PurchasePrice"] != null ? Convert.ToDecimal(item["PurchasePrice"].ToString()) : 0;
                    cmodel.DiscountRate = item["DiscountRate"] != null ? Convert.ToDecimal(item["DiscountRate"].ToString()) : 0;
                    cmodel.DeductionAmount = item["DeductionAmount"] != null ? Convert.ToDecimal(item["DeductionAmount"].ToString()) : 0;
                    cmodel.PurchaseAmount = item["PurchaseAmount"] != null ? Convert.ToDecimal(item["PurchaseAmount"].ToString()) : 0;
                    cmodel.StorageId = item["StorageId"] != null
                                           ? new Guid(item["StorageId"].ToString())
                                           : Guid.Empty;
                    #region 库存
                    var imodel = new InventoryEntity
                                     {
                                         InventoryId = model.InitPurId,
                                         GoodsId = cmodel.GoodsId,
                                         InventoryType = model.BusinessCategory,
                                         StorageId = cmodel.StorageId,
                                         InventoryQty = cmodel.Qty
                                     };
                    ilist.Add(imodel);
                    #endregion
                    list.Add(cmodel);
                }
            }
            if (!_dal.AddInitPur(model)) return;
            _idal.AddInitPurDetail(list);
            _indal.AddInventory(ilist);
        }

        public void ModifyInitPur(string data)
        {
            var model = new InitPurEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<InitPurDetailEntity>();
            var ilist = new List<InventoryEntity>();
            foreach (Hashtable row in rows)
            {
                model.InitPurId = new Guid(row["InitPurId"].ToString());
                model.VendorId = new Guid(row["VendorId"].ToString());
                model.DocDate = Convert.ToDateTime(row["DocDate"].ToString());
                model.DocNumber = row["DocNumber"].ToString();
                model.BusinessCategory = Convert.ToInt32(row["BusinessCategory"]);
                model.Remark = row["Remark"] != null ? row["Remark"].ToString() : string.Empty;
                model.DiscountRate = row["DiscountRate"] != null ? Convert.ToDecimal(row["DiscountRate"].ToString()) : 0;
                model.DeductionAmount = row["DeductionAmount"] != null ? Convert.ToDecimal(row["DeductionAmount"].ToString()) : 0;
                model.FoldAmount = row["FoldAmount"] != null ? Convert.ToDecimal(row["FoldAmount"].ToString()) : 0;
                model.Payment = row["Payment"] != null ? Convert.ToDecimal(row["Payment"].ToString()) : 0;
                model.AccountId = row["AccountId"] != null ? new Guid(row["AccountId"].ToString()) : Guid.Empty;
                model.Arrears = row["Arrears"] != null ? Convert.ToDecimal(row["Arrears"].ToString()) : 0;
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new InitPurDetailEntity();
                    cmodel.InitPurDetailId = Guid.NewGuid();
                    cmodel.InitPurId = model.InitPurId;
                    cmodel.GoodsId = new Guid(item["GoodsId"].ToString());
                    cmodel.Qty = item["Qty"] != null ? Convert.ToDecimal(item["Qty"]) : 0;
                    cmodel.PurchasePrice = item["PurchasePrice"] != null ? Convert.ToDecimal(item["PurchasePrice"].ToString()) : 0;
                    cmodel.DiscountRate = item["DiscountRate"] != null ? Convert.ToDecimal(item["DiscountRate"].ToString()) : 0;
                    cmodel.DeductionAmount = item["DeductionAmount"] != null ? Convert.ToDecimal(item["DeductionAmount"].ToString()) : 0;
                    cmodel.PurchaseAmount = item["PurchaseAmount"] != null ? Convert.ToDecimal(item["PurchaseAmount"].ToString()) : 0;
                    cmodel.StorageId = item["StorageId"] != null
                                           ? new Guid(item["StorageId"].ToString())
                                           : Guid.Empty;
                    #region 库存
                    var imodel = new InventoryEntity
                    {
                        InventoryId = model.InitPurId,
                        GoodsId = cmodel.GoodsId,
                        InventoryType = model.BusinessCategory,
                        StorageId = cmodel.StorageId,
                        InventoryQty = cmodel.Qty
                    };
                    ilist.Add(imodel);
                    #endregion
                    list.Add(cmodel);
                }
            }
            _dal.ModifyInitPur(model);
            _idal.DeleteInitPurDetail(model.InitPurId.ToString());
            _idal.AddInitPurDetail(list);
            _indal.DeleteInventory(model.InitPurId.ToString());
            _indal.AddInventory(ilist);
            #region 有时间再弄（不删除记录，批量修改库存数量）
            //if (ilist.Count > 0)
            //{

            //    _indal.ModifyInventory(ilist);
            //}
            #endregion
        }

        public bool RemoveInitPur(string id)
        {
            if (_dal.RemoveInitPur(id))
            {
                _idal.DeleteInitPurDetail(id);
                _indal.DeleteInventory(id);
            }
            return _dal.RemoveInitPur(id);
        }

        public string GetInitPurInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetInitPurInfo(id));
        }

        public decimal GetTotalInitialNumber(string goodsId, string storageId)
        {
            return _dal.GetTotalInitialNumber(goodsId, storageId);
        }
    }
}
